﻿
package extremefx.tools {
	import flash.xml.XMLNode;	
	import flash.xml.XMLDocument;	
	
	/**
	 * @author Marcelo Volmaro
	 */
	public final class XmlWalker {
		private var _theXml:XMLDocument;
		
		public function XmlWalker(pXml:XMLDocument) {
			_theXml = pXml;
			_theXml.ignoreWhite = true;
		}
		
		public function walk(pNodeIn:Function, pNodeOut:Function = null):void {
			var currentNode:XMLNode = _theXml.firstChild;
			var nextNode:XMLNode;
			
			while (currentNode) {
				if (pNodeIn != null) pNodeIn(currentNode);
				
				if ((nextNode = currentNode.firstChild)) {
					currentNode = nextNode;
					continue;
				}

				while (currentNode) {
					nextNode = currentNode;
					
					if (nextNode) {
						currentNode = nextNode;
						break;
					}

					if (currentNode == _theXml){
						currentNode = null;
						
					} else {
						currentNode = currentNode.parentNode;
						if (pNodeOut != null) pNodeOut(currentNode);
					}
				}
			}
		}
	}
}
